﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using yq.CacheHelp;

namespace yq.IBase.Basis
{
    public class DefaultCache
    {
        /// <summary>
        /// SQL缓存
        /// </summary>
        private static Dictionary<string, object> dic = new Dictionary<string, object>();

        /// <summary>
        /// 正常执行更新缓存方法
        /// </summary>
        /// <param name="fullName"></param>
        public virtual void Delete(string fullName)
        {
            dic.Remove(fullName);
        }

        /// <summary>
        /// 是否包含缓存对象
        /// </summary>
        /// <param name="key"></param>
        public virtual bool ContainsKey<T>() where T : class, new()
        {
            string fullName = new T().GetType().FullName;
            return dic.ContainsKey(fullName);
        }

        /// <summary>
        /// 写缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="info"></param>
        public virtual void Write<T>(IList<T> info) where T : class, new()
        {
            string fullName = new T().GetType().FullName;
            dic[fullName] = info;
        }

        /// <summary>
        /// 读取缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public virtual IList<T> Read<T>() where T : class, new()
        {
            string fullName = new T().GetType().FullName;
            var list = dic[fullName] as IList<T>;
            return list ?? new List<T>();
        }
    }
}
